package com.sunrise.xdoc.dao;

import java.util.Collection;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;

public interface IBaseDao<T> {

	void setEntityClass(Class<T> clazz);

	void delete(T entity);

	void saveOrUpdate(T entity);

	void saveOrUpdateAll(Collection<T> list);

	T findById(Long id);

	T findByCode(String code);

	List<T> loadAll();

	// List<T> findAllActive();

	DetachedCriteria getDetachedCriteria();

	List<T> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults);

	List<T> findByCriteria(DetachedCriteria dc);

	/**
	 * Retrieves the first of domain objects matching the Hibernate criteria.
	 * 
	 * @param dc
	 *            the criteria that the result has to fulfill <b>Note: Do not
	 *            reuse criteria objects! They need to recreated (or cloned e.g.
	 *            using <tt>SerializationUtils.clone()</tt>) per execution, due
	 *            to the suboptimal design of Hibernate's criteria facility.</b>
	 * @return the first of objects that fulfill the criteria
	 */
	T findFirstByCriteria(DetachedCriteria dc);

	/**
	 * Retrieves the number of domain objects matching the Hibernate criteria.
	 * 
	 * @param hibernateCriteria
	 *            the criteria that the result has to fulfill <b>Note: Do not
	 *            reuse criteria objects! They need to recreated (or cloned e.g.
	 *            using <tt>SerializationUtils.clone()</tt>) per execution, due
	 *            to the suboptimal design of Hibernate's criteria facility.</b>
	 * @return the number of objects that fulfill the criteria
	 * @throws DataAccessException
	 * 
	 * @see ConvenienceHibernateTemplate#findCountByCriteria(DetachedCriteria)
	 */
	long findCountByCriteria(DetachedCriteria hibernateCriteria) throws DataAccessException;

	/**
	 * Get number of all rows in the table database.
	 * 
	 * @return
	 */
	long getRowCount();

	void deleteAll(Collection<T> entities);

	void flush();

	/**
	 * Get the next sequence of the <code>name</code>. If the sequence of
	 * <code>name</code> is not in the sequence table yet, it will automatically
	 * insert a row in to the table. It may raise the duplicate constraint
	 * exception when there are 2 users initialize the <code>name</code> at the
	 * same time.
	 * 
	 * @return the next sequence of <code>name</code>
	 */
	long getNextSequence(String name);

	Session getCurrentSession();
}
